import { ConverterBase } from './base';
import { ProgressBar, Sprite, UITransform } from './cc';
import { enum_ } from '../../model';

export class ProgressBarConverter extends ConverterBase {
    public static prefix = 'pb-';

    public async convert() {
        const node = this.createNode();
        const pb = this.appendComponent(ProgressBar, node);
        pb.progress = 0.5;
        const children = this.treePSD.children();
        for (const r of children) {
            if (r.get<string>(enum_.Psd.name).includes('.')) {
                pb.totalLength = r.get<number>(enum_.Psd.width);
                await this.appendSprite(r, node);
            } else {
                const childNode = await this.factory.build(r).convert();
                childNode.name = 'p-bar';
                childNode.parent = node;
                const sprite = childNode.getComponent(
                    ConverterBase.getCcPropFunc(Sprite.name),
                );
                pb.barSprite = sprite;
            }
        }

        if (pb.barSprite) {
            pb.barSprite.node.position.y = -node.getComponent(
                ConverterBase.getCcPropFunc(UITransform.name),
            ).contentSize.y / 2;
        }

        return node;
    }
}